﻿using Electronic_work_order.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace Electronic_work_order.Interfacess.OA_Organization
{
    public class Organization_Department : InterFace_Organization
    {
        private readonly AppDbContext _context;

        public Organization_Department(AppDbContext context)
        {
            _context = context;
        }
        //添加职位
        public int AddDepartment(Position position)
        {
         
            _context.Position.Add(position);
            return _context.SaveChanges();
        }

        //添加部门(Organization)
        public int AddPosition(Organization organization)
        {
         
            _context.organizations.Add(organization);
            return _context.SaveChanges();
        }
        //查询所有部门
        public IEnumerable<Organization> GetOrganizationName()
        {
            return _context.organizations.ToList();
        }
        //查询的是部门和部门下的所有职位
        public IEnumerable<Organization> GetOrganizations()
        {
          return  _context.organizations.Include(a=>a).ToList();
        }

        public IEnumerable<Position> GetPosition(int ParentId)
        {
            return _context.Position.Where(a => a.ParentID == ParentId).ToList();
        }


        //查询所有人员并查询其职位
        public IEnumerable<Users> GetUsers()
        {
            return _context.Users.Include(a=>a.Position).ToList();
        }
        //查询部门和职位
        public IEnumerable<object> GetOrageUserName(int OrganizationID)
        {
            var orageUsrs = (from Users in _context.Users
                             join Position in _context.Position
                              on Users.PositionID equals Position.PositionID
                             join Organization in _context.organizations on
                             Position.ParentID equals Organization.OrganizationID
                             join UserRole in _context.UserRoles on Users.RoleID equals UserRole.RoleID
                             where Organization.OrganizationID == OrganizationID
                             select new
                             {
                                 Organization.OrganizationID,
                                 Organization.OrganizationName,
                                 Organization.Update_date,
                                 Position.PositionID,
                                 Position.PositionName,
                                 Position.ParentID,
                                 Position.Grade,
                                 Users.UsersID,
                                 Users.UsersName,
                                 Users.UsersPwd,
                                 Users.UsersSex,
                                 Users.UsersPhone,
                                 Users.UsersEmail,
                                 Users.RoleID,
                                 Users.EntryTime,
                                 Users.Nickname,
                                 Users.StateID,
                                 UserRole.RoleName
                             });
            return orageUsrs;
        }

    }
}
